// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.
<%# encoding: utf-8%>
$(function () {
    if ($('body').hasClass('sla_template')) {
        $('#slas-menu-link').addClass('menu-current');
        slasTable_Settings = {
            'bAutoWidth': false,
            'bLengthChange': false,
            'bStateSave': true,
            'oLanguage': dataTables_lang,
            'sPaginationType': 'full_numbers',
            'aoColumnDefs': [
                {'bSearchable': false, 'aTargets': [2, 3]},
                {'bSortable': false, 'aTargets': [3]},
                {'sType': 'string', 'aTargets': [2]}
            ],
            'fnRowCallback':   function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {
                var appid = '#' + nRow.id + '-tooltip';
                $(appid).tooltip({'tip': appid + 'box'});
                return nRow;
            },
            'fnDrawCallback':  function() {
                addSla_instance_trigger = $('.create_slainstance').overlay({
                    'closeOnClick': false,
                    'mask': {color: '#404040', loadSpeed: 'normal', opacity: 0.7},
                    'top': '25%',
                    'onBeforeLoad': function(event) {
                        $('.modalBox-flash').empty();
                        var form = this.getOverlay().find("#new-sla_instance-form");
                        form.attr("action", this.getTrigger().attr("href"));
                    }
                });
            }
        };

        slasinstacneTable_Settings = {
            'bAutoWidth': false,
            'bLengthChange': false,
            'bStateSave': true,
            'oLanguage': dataTables_lang,
            'sPaginationType': 'full_numbers',
            'aoColumnDefs': [
                {'bSearchable': false, 'aTargets': [2, 3]},
                {'bSortable': false, 'aTargets': [3]},
                {'sType': 'string', 'aTargets': [2]}
            ]


        };
        slatemplatesTable = $('#sla_templates-table').dataTable(slasTable_Settings);
        slainstancesTable = $('#sla_instances-table').dataTable(slasinstacneTable_Settings);


        var ultabs = $('ul.tabs');
        ultabs.tabs('div.panels > div.panel', {
            effect: 'slide',
            tabs: 'li',
            onClick: function (event, tabIndex) {
                var tab = this.getCurrentTab().find('a').attr('href');
                if (tab == '#sla_templates_admin') {
                    $('.invoke-modalBox').html('<button class="addSlaTemplate" rel="#add_sla_template_modalBox">' + I18n.t('sla_templates.js.add_sla') + '</button>').show();
                    addSla_template_trigger = $('.addSlaTemplate').overlay({
                        'closeOnClick': false,
                        'mask': {color: '#404040', loadSpeed: 'normal', opacity: 0.7},
                        'top': '25%',
                        'onBeforeLoad': function (event) {
                            $('.modalBox-flash').empty();
                        }
                    });
                    $('#sla_templates-table .icon-tip').tooltip({'effect': 'fade', 'tipClass': 'help-tip'});
                }

                if (tab == '#sla_instances') {
                    $('.invoke-modalBox').html('').show();

                }
                if (tab == '#sla_instances') {
                    $('.invoke-modalBox').html('').show();

                }


            }
        });


        $('#new-sla_template-form .help').tooltip({'effect': 'fade', 'tipClass': 'help-tip'});
        $('#new-sla_template-form').validator({'effect': 'below_field_error', 'inputEvent': 'blur', 'lang': validatorTools_lang});
        $(document)
                .on('ajax:beforeSend', '#new-sla_template-form', function (evt, xhr, settings) {
                    var validator = $('#new-sla_template-form').data('validator');
                    if (!validator.checkValidity()) return false;
                    request_processed = false;
                    $(this).find('.modalBox-submit input[type="submit"]').attr({value: I18n.t('assets.js.ajax_processing')});
                })
                .on('ajax:success', '#new-sla_template-form', function (evt, data, status, xhr) {
                    if (request_processed == true) {
                        $(this).find('input[type="check_box"]').val('');

                    }
                })
                .on('ajax:error', '#new-sla_template-form', function (evt, xhr, status, error) {
                    request_processed = true;
                    $('#flash-content').empty();
                    $('#flash-content').append('<div class="alert">' + I18n.t('assets.js.ajax_error') + '</div>');
                    $('#flash').fadeIn('slow');
                })
                .on('ajax:complete', '#new-sla_template-form', function (evt, xhr, status) {
                    if (request_processed == true) {
                        addSla_template_trigger.eq(0).overlay().close();
                    }
                    $(this).find('.modalBox-submit input[type="submit"]').attr({value: I18n.t('sla_templates.js.add_sla')});
                });


        $(document)
                .on('ajax:beforeSend', '#delete-sla-link', function (evt, xhr, settings) {
                    $(this).find('img').attr({ src: '<%= asset_path('wait.gif') %>'}).removeClass('delete');
                })
                .on('ajax:error', '#delete-sla-link', function (evt, xhr, status, error) {
                    $('#flash-content').empty();
                    $('#flash-content').append('<div class="alert">' + I18n.t('assets.js.ajax_error') + '</div>');
                    $('#flash').fadeIn('slow');
                })
                .on('ajax:complete', '#delete-sla-link', function (evt, xhr, status) {
                    $(this).find('img').attr({ src: '<%= asset_path('s.gif') %>'}).addClass('delete');
                });

        $(document)
                .on('ajax:beforeSend', '#delete-sla_instance-link', function (evt, xhr, settings) {
                    $(this).find('img').attr({ src: '<%= asset_path('wait.gif') %>'}).removeClass('delete');
                })
                .on('ajax:error', '#delete-sla_instance-link', function (evt, xhr, status, error) {
                    $('#flash-content').empty();
                    $('#flash-content').append('<div class="alert">' + I18n.t('assets.js.ajax_error') + '</div>');
                    $('#flash').fadeIn('slow');
                })
                .on('ajax:complete', '#delete-sla_instance-link', function (evt, xhr, status) {
                    $(this).find('img').attr({ src: '<%= asset_path('s.gif') %>'}).addClass('delete');
                });


        $('#new-sla_instance-form .help').tooltip({'effect': 'fade', 'tipClass': 'help-tip'});
        $('#new-sla_instance-form').validator({'effect': 'below_field_error', 'inputEvent': 'blur', 'lang': validatorTools_lang});
        $(document)
                .on('ajax:beforeSend', '#new-sla_instance-form', function(evt, xhr, settings) {
                    var validator = $('#new-sla_instance-form').data('validator');
                    if (!validator.checkValidity()) return false;
                    request_processed = false;
                    $(this).find('.modalBox-submit input[type="submit"]').attr({value: I18n.t('assets.js.ajax_processing')});
                })
                .on('ajax:success', '#new-sla_instance-form', function(evt, data, status, xhr) {
                    if (request_processed == true) {
                        $(this).find('input[type="url"],input[type="text"]').val('');
                    }
                })
                .on('ajax:error', '#new-sla_instance-form', function(evt, xhr, status, error) {
                    $('#flash-content').empty();
                    $('#flash-content').append('<div class="alert">' + I18n.t('assets.js.ajax_error') + '</div>');
                    $('#flash').fadeIn('slow');
                })
                .on('ajax:complete', '#new-sla_instance-form', function(evt, xhr, status) {
                    if (request_processed == true) {
                        addSla_instance_trigger.each(function() { $(this).overlay().close(); });
                    }
                    $(this).find('.modalBox-submit input[type="submit"]').attr({value: I18n.t('sla_templates.js.add_sla')});
                });


    }
});
